package com.atguigu.c_designed.m2stucturalModel.s3Decoretor;

public class DecoretorFastFood {
    public static void main(String[] args) {
        // 来一份炒饭
        FastFood firedrice = new FrideRice();
        System.out.println(firedrice.getName()+" "+firedrice.getTotalPrice());
        // 来一份炒饭 加蛋
        firedrice = new Egg(firedrice);
        System.out.println(firedrice.getName()+" "+firedrice.getTotalPrice());
        // 来一份炒饭  加蛋 加蛋
        firedrice = new Egg(firedrice);
        System.out.println(firedrice.getName()+" "+firedrice.getTotalPrice());
        // 来一份炒饭  加蛋 加蛋 加火腿
        firedrice = new Ham(firedrice);
        System.out.println(firedrice.getName()+" "+firedrice.getTotalPrice());
    }
}

//被装饰类的主体
abstract class FastFood{
    private String name;
    private float price;

    public abstract float getTotalPrice();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
}

//建立缓冲层，抽取公共属性和方法
class Food extends FastFood{
    @Override
    public float getTotalPrice() {
        return getPrice();
    }
}
// 创建具体的被装饰的类
class FrideRice extends Food{

    public FrideRice() {
        setName("炒饭。。");
        setPrice(10f);
    }
}
// 创建具体的被装饰的类
class FrideNoodles extends Food{

    public FrideNoodles() {
        setName("炒面。。");
        setPrice(10f);
    }
}
// 创建装饰着父类，抽取公共方法
class Flavor extends FastFood{

    //组合被装饰的类
    private FastFood fastFood;

    public Flavor(FastFood fastFood) {
        this.fastFood = fastFood;
    }

    @Override
    public float getTotalPrice() {
        return getPrice()+fastFood.getTotalPrice();
    }

    @Override
    public String getName() {
        return fastFood.getName() + super.getName();
    }
}
// 具体的装饰类
class Egg extends Flavor{
    public Egg(FastFood fastFood) {
        super(fastFood);
        setPrice(1f);
        setName("鸡蛋加一个。。。");
    }
}
// 具体的装饰类
class Ham extends Flavor{
    public Ham(FastFood fastFood) {
        super(fastFood);
        setPrice(2f);
        setName("火腿搞一个。。");
    }
}